<%--
  Created by IntelliJ IDEA.
  User: 86183
  Date: 2022/5/4
  Time: 0:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>个人中心</title>
    <link rel="shortcut icon" href="#"/>
    <style>
        body{
            color: white;
            background-color: black
        }
    </style>
</head>
<body>
<div>
    <div>
        <button id="detailUser">个人信息</button>
        <button id="collection">收藏</button>
        <button id="orders">订单</button>
        <button id="tickets">抵用券</button>
        <button id="cars">购物车</button>
        <button id="return">返回</button>
    </div>
    <div id="show">

    </div>
</div>
</body>
<script>
    let Orders;
    let Collections;
    function getCookie(name) {
        if (document.cookie.length > 0) {
            let start = document.cookie.indexOf(name + "=");
            if (start != -1) {
                start = start + name.length + 1;
                let end = document.cookie.indexOf(";", start);
                if (end == -1) end = document.cookie.length;
                return decodeURI(document.cookie.substring(start, end));
            }
        }
        return null;
    }
    let params = new URLSearchParams(location.search);
    let state = params.get('state');
    if(state!=null){
        console.log(state);
        if(state=="SUCCESS"){
            alert("更新成功!")
            if(getCookie("role")=="商家"){
                window.location.href="Seller.jsp";
            }
        }else{
            console.log("fail")
            alert(state);
            alert("操作失败,请稍后再试")
        }
    }
    document.getElementById("return").onclick=function (){
        window.location="index.jsp";
    }
    function showDetailUser(json){
        document.getElementById("show").innerHTML='';
        document.getElementById("show").innerHTML=
            '<img border="0" src=""  id="picture" alt="picture" width="200" height="200"><br>' +
            '昵称:<text id="name">'+getCookie("name")+'</text><br>'+
            '电话:<text id="telephone">'+json['telephone']+'</text><br>'+
            '资产: <text id="money">'+json['money']+'</text> 元<br>'+
            '地址:<text id="district">'+json['districtName']+"</text><br>" +
            '<text id="street">'+json['streetName']+'</text><text id="address">'+json['detailedAddress']+
            '</text><br><button id="update" onclick="updateDetailUser()">设置</button>';
        document.getElementById("picture").src="http://localhost/picture/upload/"+json["picture"];
    }
    function updateDetailUser() {
        let name = document.getElementById("name").textContent;
        let telephone = document.getElementById("telephone").textContent;
        let district = document.getElementById("district").textContent;
        let street = document.getElementById("street").textContent;
        let address = document.getElementById("address").textContent;
        let money = document.getElementById("money").textContent;
        let picture=document.getElementById("picture").src;
        window.location="UpdateSomething.jsp?name="+name+"&telephone="+telephone+"&district="+
            district+"&street="+street+"&address="+address+"&money="+money+"&picture="+picture;
    }
    document.getElementById("detailUser").onclick=function (){
        let json={
            "id":getCookie("id"),
            "mode": "detailUser",
            "method":"DetailUser"
        }
        console.log("json:"+JSON.stringify(json))
        let xhr=new XMLHttpRequest();
        xhr.open("post","http://localhost:80/homePage",true);
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
        xhr.responseType='json';
        xhr.send("json="+JSON.stringify(json));
        xhr.onreadystatechange=function(){;
            if(xhr.status==200 && xhr.readyState==4){
                let data = xhr.response;
                console.log(data);
                if(data["code"]==404){
                    document.getElementById("show").innerHTML='';
                    document.getElementById("show").innerHTML=
                    `<button id="update" onclick="insert()">设置</button>`;
                }else{
                    console.log(JSON.parse(data.data));
                    showDetailUser(JSON.parse(data.data));
                }
            }
        }
    }
    function insert(){
        window.location="UpdateSomething.jsp";
    }
    function showCollections(){
        document.getElementById("show").innerHTML='';
        for(let i=0;i<Collections.length;i++){
            document.getElementById("show").innerHTML+='收藏店铺:<text>'+Collections[i]['shopName']+'</text>' +
                '<button id="'+i+'" onclick="deleteCollection('+i+')">取消收藏</button><br>';
        }
    }
    function deleteCollection(foot){
        let json={
            "id":getCookie("id"),
            "collectionId":Collections[foot]["id"],
            "shopId":Collections[foot]["shopId"],
            "mode": "updateCollection"
        }
        console.log("json:"+JSON.stringify(json))
        let xhr=new XMLHttpRequest();
        xhr.open("post","http://localhost:80/homePage",true);
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
        xhr.responseType='json';
        xhr.send("json="+JSON.stringify(json));
        xhr.onreadystatechange=function(){
            if(xhr.status==200 && xhr.readyState==4){
                let data = xhr.response;
                console.log(data);
                console.log(data.state);
                alert(data.state);
                window.location="Something.jsp";
            }
        }
    }
    document.getElementById("collection").onclick=function (){
        let json={
            "id":getCookie("id"),
            "shopId":null,
            "mode": "collection"
        }
        console.log("json:"+JSON.stringify(json))
        let xhr=new XMLHttpRequest();
        xhr.open("post","http://localhost:80/homePage",true);
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
        xhr.responseType='json';
        xhr.send("json="+JSON.stringify(json));
        xhr.onreadystatechange=function(){;
            if(xhr.status==200 && xhr.readyState==4){
                let data = xhr.response;
                console.log(data);
                if(data["code"]==404){
                    alert(data.state);
                }else{
                    Collections=JSON.parse(data.data);
                    console.log(Collections);
                    showCollections();
                }
            }
        }
    }
    function getAllOrders(){
        let json={
            "id":getCookie("id"),
            "mode": "orders",
            "state": null
        }
        return json;
    }
    function getOverOrders(){
        let json={
            "id":getCookie("id"),
            "mode": "orders",
            "state": "已完成"
        }
        return json;
    }
    function getIngOrders(){
        let json={
            "id":getCookie("id"),
            "mode": "orders",
            "state": "未接单"
        }
        return json;
    }
    function senOrders(json){
        console.log("json:"+JSON.stringify(json))
        let xhr=new XMLHttpRequest();
        xhr.open("post","http://localhost:80/homePage",true);
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
        xhr.responseType='json';
        xhr.send("json="+JSON.stringify(json));
        xhr.onreadystatechange=function(){;
            if(xhr.status==200 && xhr.readyState==4){
                let data = xhr.response;
                console.log(data);
                if(data["code"]==404){
                    console.log(data.state);
                    alert("这里空空如也呢!")
                }else{
                    console.log(JSON.parse(data.data));
                    Orders=JSON.parse(data.data);
                    document.getElementById("show").innerHTML='';
                    for(let i=Orders.length-1;i>=0;i--){
                        document.getElementById("show").innerHTML+=
                            '内容: '+Orders[i]['content']+'<br>'+
                            '店铺: '+Orders[i]['shopName']+'<br>'+
                            '价格: '+Orders[i]['cost']+' 元<br>'+
                            '状态: '+Orders[i]['state'];
                        if(Orders[i]['state']=='未接单'){
                            document.getElementById("show").innerHTML+=
                                '<button id="'+i+'" style="margin-left: 5px"' +
                                ' onclick="cancelOrders('+i+')">取消订单</button>'
                        }
                        document.getElementById("show").innerHTML+=
                            '<br>点餐时间:'+Orders[i]['orderTime']+'<br>'+
                            '完成时间:'+Orders[i]['updateTime']+'<br>'+'<hr>';
                    }
                }
            }
        }
    }
    function cancelOrders(i){
        let orders={
            "id":getCookie("id"),
            "ordersId":Orders[i]["id"],
            "state":"已取消",
            "mode":"updateOrders"
        }
        console.log("json="+JSON.stringify(orders))
        let xhr=new XMLHttpRequest();
        xhr.open("post","http://localhost:80/homePage",true);
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
        xhr.responseType='json';
        xhr.send("json="+JSON.stringify(orders));
        xhr.onreadystatechange=function(){
            if(xhr.status==200 && xhr.readyState==4){
                let data = xhr.response;
                console.log(data);
                console.log(data.state);
                alert(data.state);
                window.location="Something.jsp";
            }
        }

    }
    function showOrders(){
        document.getElementById("show").innerHTML='';
        document.getElementById("show").innerHTML+=
            '<button id="all" onclick="senOrders(getAllOrders())">所有订单</button>' +
            '<button id="over" " onclick="senOrders(getOverOrders())">已完成订单</button>' +
            '<button id="ing"  onclick="senOrders(getIngOrders())">进行中订单</button>';
    }
    document.getElementById("orders").onclick=function (){
        showOrders()
    }
    document.getElementById("tickets").onclick=function (){
        let json={
            "id":getCookie("id"),
            "mode": "tickets"
        }
        console.log("json:"+JSON.stringify(json))
        let xhr=new XMLHttpRequest();
        xhr.open("post","http://localhost:80/homePage",true);
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
        xhr.responseType='json';
        xhr.send("json="+JSON.stringify(json));
        xhr.onreadystatechange=function(){
            if(xhr.status==200 && xhr.readyState==4){
                let data = xhr.response;
                console.log(data);
                if(data["code"]==404){
                    console.log(data.state);
                    alert(data.state);
                }else{
                    let tickets=JSON.parse(data.data);
                    console.log(JSON.parse(data.data));
                    document.getElementById("show").innerHTML='';
                    for(let i=tickets.length-1;i>=0;i--){
                        document.getElementById("show").innerHTML+=
                            '店铺:<text>'+tickets[i]['shopName']+'</text><br>'+
                            '折扣:<text>'+tickets[i]['discount']+'折</text><br>'+
                            '状态:<text>'+tickets[i]['state']+'</text><br>'+
                            '有效期:至 <text>'+tickets[i]['deadline']+'</text><br>'+'<hr>';

                    }
                }
            }
        }
    }
    function homePage(mode){
        let json={
            "id":getCookie("id"),
            "mode": mode
        }
        console.log("json:"+JSON.stringify(json))
        let xhr=new XMLHttpRequest();
        xhr.open("post","http://localhost:80/homePage",true);
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
        xhr.responseType='json';
        xhr.send("json="+JSON.stringify(json));
        xhr.onreadystatechange=function(){
            if(xhr.status==200 && xhr.readyState==4){
                let data = xhr.response;
                console.log(data);
                if(data["code"]==404){
                    console.log(data.state);
                    alert(data.state);
                }else{
                    console.log(data.data);
                    let cars=JSON.parse(data.data);
                    console.log(cars)
                    document.getElementById("show").innerHTML='';
                    for(let i=0;i<cars.length;i++){
                        document.getElementById("show").innerHTML+=
                            '内容:<text>'+cars[i]['content']+'</text><br>'+
                            '店铺:<text>'+cars[i]['shopName']+'</text><br>'+
                            '价格:<text>'+cars[i]['price']+'</text><br>'+
                            '状态:<text>'+cars[i]['state']+'</text><br>'+'<hr>';

                    }
                }
            }
        }
    }
    document.getElementById("cars").onclick=function (){
        homePage("cars")
    }
</script>
</html>
